package com.users.service.impl;

import com.commons.entity.MovieUserInfo;
import com.commons.utils.AutoGenerateIds;
import com.commons.utils.AvatarRandom;
import com.commons.utils.MD5Tools;
import com.commons.vo.UpdatePwdVo;
import com.users.dao.MovieUsersDao;
import com.users.service.MovieUsersServices;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import rx.Single;

import javax.annotation.Resource;
import javax.management.monitor.Monitor;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author mis_wu
 * @date 2022/3/13 12:01
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class MovieUsersServicesImpl implements MovieUsersServices {

    @Resource
    private MovieUsersDao movieUsersDao;

    /**
     * login
     * @param name
     * @return
     */
    @Override
    public MovieUserInfo movieUserLogin(String name) {
        return movieUsersDao.movieUserLogin(name);
    }

    /**
     * 注册
     * @param movieUserInfo
     * @return
     */
    @Override
    public synchronized Integer movieUserRegister(MovieUserInfo movieUserInfo){
        MovieUserInfo user = getUserByName(movieUserInfo.getUserName());
        //用户不存在
        if (ObjectUtils.isEmpty(user)){
            //密码加密
            String pwd = DigestUtils.md5DigestAsHex(movieUserInfo.getUserPassword().getBytes());
            movieUserInfo.setUserPassword(pwd);
            movieUserInfo.setUserId(AutoGenerateIds.getGenerateId(1).get(0));
            movieUserInfo.setUserAvatar(AvatarRandom.getAvatarPath());
            movieUserInfo.setUserState(0);
            movieUserInfo.setRegisterTime(new Date());
            if (StringUtils.isEmpty(movieUserInfo.getUserDescript())){
                movieUserInfo.setUserDescript("恭喜您,注册成功;赶紧修改描述吧!!!!");
            }
            return movieUsersDao.movieUserRegister(movieUserInfo);
        }
        return -1;

    }

    /**
     * 查询user
     * @param name
     * @return
     */
    public MovieUserInfo getUserByName(String name){
        return movieUsersDao.getUserByName(name);
    }


    /**
     * 修改密码
     * @param updatePwdVo
     * @return
     */
    @Override
    public synchronized Boolean getMovieUserByUserId(UpdatePwdVo updatePwdVo) {
        Boolean flag = false;
        MovieUserInfo userInfo = movieUsersDao.getMovieUserByUserId(updatePwdVo.getUserId(), updatePwdVo.getUserName().trim());
        if (!ObjectUtils.isEmpty(userInfo)){
            //加密
            String bytes = DigestUtils.md5DigestAsHex(updatePwdVo.getOldPwd().getBytes());
            log.info("db密码:{}<===>前端原密码:{}<===>新密码:{}",userInfo.getUserPassword(),updatePwdVo.getOldPwd(),updatePwdVo.getNewPwd());
            if (StringUtils.equals(bytes,userInfo.getUserPassword())){
                //新密码加密 修改
                String newPwd = DigestUtils.md5DigestAsHex(updatePwdVo.getNewPwd().getBytes());
                updatePwdVo.setNewPwd(newPwd);
                flag = updatePwd(updatePwdVo);
            }
        }
        return flag;
    }

    /**
     * 修改
     * @param updatePwdVo
     * @return
     */
    public Boolean updatePwd(UpdatePwdVo updatePwdVo){
        return movieUsersDao.updatePwd(updatePwdVo);
    }

    /**
     * 修改用户信息
     * @param userName
     * @param descript
     * @return
     */
    @Override
    public Boolean updateUserInfoById(Long userId ,String userName, String descript) {
        log.info("修改信息--id:{},name:{},desc:{}",userId,userName,descript);
        if (ObjectUtils.isNotEmpty(userId)){
            if (StringUtils.isNotBlank(userName) || StringUtils.isNotBlank(descript)){
                return movieUsersDao.updateUserInfoById(userId,userName,descript);
            }
        }
        return false;
    }

}
